<?php

namespace app\admin\model;

use think\Model;

/**
 * 订单
 * @package app\admin\model
 */
class Order extends Model
{
    /**
     * 总支付金额
     * @param array $where
     * @return float
     */
    public static function orderTotalPayPrice($where)
    {
        return self::where('status', 1)->where($where)->sum('price');
    }

    /**
     * 总支付金额（折线图）
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function orderTotalPayPriceLine($startTime, $endTime)
    {
        $data = self::fieldRaw("SUM(price) total,pay_time as create_time")
            ->where('status', 1)
            ->whereTime('pay_time', '>=', $startTime)
            ->whereTime('pay_time', '<', $endTime)
            ->group('pay_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 订单总数
     * @param array $where
     * @return float
     */
    public static function orderTotalNums($where = [])
    {
        return self::where('status', 1)->where($where)->count();
    }

    /**
     * 当日支付金额
     * @param array $where
     * @return float
     */
    public static function orderTodayPayPrice($where = [])
    {
        return self::where('status', 1)->where($where)->whereDay('pay_time')->sum('price');
    }

    /**
     * 当日订单总数
     * @param array $where
     * @return float
     */
    public static function orderTodayTotalNums($where = [])
    {
        return self::where('status', 1)->where($where)->whereDay('pay_time')->count();
    }

    /**
     * 总退款金额
     * @param array $where
     * @return float
     */
    public static function orderTotalRefundPrice($where)
    {
        return self::where('status', 2)->where($where)->sum('price');
    }

    /**
     * 总退款金额（折线图）
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function orderTotalRefundPriceLine($startTime, $endTime)
    {
        $data = self::fieldRaw("SUM(price) total,refund_time as create_time")
            ->where('status', 2)
            ->whereTime('refund_time', '>=', $startTime)
            ->whereTime('refund_time', '<', $endTime)
            ->group('refund_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 当日退款金额
     * @return float
     */
    public static function orderTodayRefundPrice()
    {
        return self::where('status', 2)->whereDay('refund_time')->sum('price');
    }

    /**
     * 社群地区分布
     * @return mixed
     */
    public static function getOrderArea()
    {
        $data = self::fieldRaw("COUNT(*) nums,city as name")
            ->where('status', 1)
            ->group('city')
            ->order('nums', 'desc')
            ->limit(6)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 订单统计
     * @param $where
     * @return int
     */
    public static function countOrder($where)
    {
        return self::where($where)->where('status', 1)->count();
    }

    /**
     * 单订单数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findOrder($where)
    {
        $info = self::where($where)->find();

        return $info ? $info->toArray() : [];
    }

    /**
     * 营销中心-下单地区分布
     * @param $where
     * @param int $offset
     * @param int $length
     * @return array
     */
    public static function getMarketingOrderArea($where, $offset = 0, $length = 6)
    {
        $data = self::alias('o')
            ->join('marketing_users_cp m', 'm.mid = o.marketing_id', 'left')
            ->fieldRaw("COUNT(distinct o.uid) nums,o.city as name")
            ->where([
                ['o.marketing_id', '>', 0],
                ['o.status', '=', 1],
            ])
            ->where($where)
            ->group('o.city')
            ->order('nums', 'desc')
            ->limit($offset, $length)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 营销中心-下单地区统计
     * @param $where
     * @return int
     */
    public static function totalMarketingOrderArea($where = [])
    {
        return self::alias('o')
            ->join('marketing_users_cp m', 'm.mid = o.marketing_id', 'left')
            ->where($where)
            ->count('distinct o.city');
    }

    /**
     * 营销中心-金额累计
     * @param array $where
     * @return mixed
     */
    public static function sumMarketingOrderPrice($where = [])
    {
        return self::alias('o')
            ->join('marketing_users_cp m', 'm.mid = o.marketing_id', 'left')
            ->where([
                ['o.marketing_id', '>', 0],
                ['o.status', '=', 1],
            ])
            ->where($where)
            ->sum('price');
    }

    /**
     * 营销中心-过去7天活动金额
     * @param $where
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getMarketingOrderPrice($startTime, $endTime, $where)
    {
        $data = self::alias('o')
            ->join('marketing_users_cp m', 'm.mid = o.marketing_id', 'left')
            ->fieldRaw("SUM(o.price) total,o.pay_time as create_time")
            ->where($where)
            ->where([
                ['o.marketing_id', '>', 0],
                ['o.status', '=', 1],
            ])
            ->whereTime('o.pay_time', '>=', $startTime)
            ->whereTime('o.pay_time', '<', $endTime)
            ->group('o.pay_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 过去指定时间内订单（折线图）
     * @param $where
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getOrderPriceLine($where, $startTime, $endTime)
    {
        $data = self::fieldRaw("SUM(price) total,pay_time as create_time")
            ->where($where)
            ->where('status', 1)
            ->whereTime('pay_time', '>=', $startTime)
            ->whereTime('pay_time', '<', $endTime)
            ->group('pay_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 营销中心-转换用户订单
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getConvertMarketingOrder($where)
    {
        $data = self::alias('o')
            ->field('o.*')
            ->where($where)
            ->where('o.status', 1)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 订单列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getAllOrderList($where, $page, $pagesize)
    {
        $data = self::alias('o')
            ->join('users u', 'o.uid = u.id', 'left')
            ->join('member_level l', 'o.mid = l.id', 'left')
            ->join('order_type t', 'o.type_id = t.id', 'left')
            ->where($where)
            ->where('o.is_original', 0)
            ->order('o.create_time', 'desc')
            ->field('o.*,u.id uuid,u.nickname,u.headimg,u.role,u.associate,l.id as lid,l.level,t.name as tname')
            ->page($page, $pagesize)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 订单列表统计
     * @param $where
     * @return int
     */
    public static function countAllOrderList($where)
    {
        return self::alias('o')
            ->where($where)
            ->where('o.is_original', 0)
            ->count();
    }

    /**
     * 统计所有订单
     * @return int
     */
    public static function countAllOrder()
    {
        return self::where([
            ['is_original', '=', 0],
            ['status', '<>', -1],
        ])->count();
    }

    /**
     * 订单创建
     * @param $data
     * @return Order|Model
     */
    public static function createOrder($data)
    {
        return self::create($data);
    }
}